<?php
/**
 * @Author: xuzhiyi
 * @Date:   2016-02-14 15:19:05
 * @Last Modified by:   xuzhiyi
 * @Last Modified time: 2016-03-01 11:29:46
 * ------------------------------------------------ *
 * Index                                           *
 * ------------------------------------------------ */
namespace Admin\Controller;

class IndexController extends CommonController {
    public function index(){
        if(IS_AJAX){
            $getDate=$this->getDate();
            //dump($getDate);
            $this->ajaxReturn($getDate);
        }
        
        $info = $this->getSum();
        $goods =D('goods')->getGoodsPendding();
        $shops =D('shops')->getShopsPendding();
        //dump($goods);
        $this->assign('shops',$goods);
        $this->assign('goods',$goods);
        $this->assign('info',$info);
    	$this->assign('authId',-1);
    	$this->assign('authName','仪表台');
        $this->display();
    }
    public function about(){
    	$this->assign('authId',-2);
    	$this->assign('authName','团队简介');
        $this->display();
    }
    //待审核数据
    public function getNum(){
        $getNum['gnum']=D('Goods')->getPenddingGoodsNum();
        $getNum['snum']=D('Shops')->getPenddingShopsNum();
        $this->ajaxReturn($getNum);
    }
    //修改登录职员密码
    public function editPass(){
        if(IS_AJAX){
            switch(I('ajax')){
                case "checkPwd":
                    $loginName =  session('admin.loginName');
                    $loginPwd = I('oldLoginPwd');
                    $manager = M('managers')->where(array('loginName'=>$loginName))->find();
                    
                    //密码判断
                    if ($manager['loginPwd'] != md5(I('oldLoginPwd') . $manager['secretKey'])) {
                        $this->ajaxReturn(false);                       
                    }else{
                        $this->ajaxReturn(true);
                    }
                    break;
                default:
                    $loginName =  session('admin.loginName');
                    $loginPwd = I('newLoginPwd');
                    $m=M('managers');
                    $manager = $m->where(array('loginName'=>$loginName))->find();
                    $data['loginPwd']=  md5($loginPwd . $manager['secretKey']);                         
                    $rs=$m->where(array('loginName'=>$loginName))->save($data);
                    if(false!=$rs){
                        $this->success('密码修改成功','',true);
                    }
                    $this->success('密码修改失败','',true);
            }
        }
        
    } 

    //统计信息

    public function getSum(){
        $info = array();
        $info['userSum'] = M('Users')->where('userFlag=1')->count();//新增用户
        //申请店铺
        $info['shopApplySum'] = M('Shops')->where('shopStatus = 0 and shopFlag=1')->count();
        //商品
        $info['goodsSum'] = M('goods')->where('goodsFlag=1')->count();
        //订单
        $info['orderSum'] = M('orders')->where('orderFlag=1 and orderStatus >=0')->count();
        //订单总金额
        $info['moneySum'] = M('orders')->where('orderFlag=1 and orderStatus >=0')->sum('totalMoney');       
        //店铺
        $info['shopSum'] = M('Shops')->where('shopStatus = 1 and shopFlag=1')->count();
        return $info;
    }

//一周动态

    public function getWeek(){
        $info = array();
        //用户
        $weekDate = date('Y-m-d 00:00:00',time()-604800);//一周内
        $info['userNew'] = M('Users')->where('userFlag=1 and createTime>"'.$weekDate.'"')->count();//新增用户
        
        //申请店铺
        $info['shopApply'] = M('Shops')->where('shopStatus >= 0 and shopFlag=1 and createTime>"'.$weekDate.'"')->count();
        
        //新增商品
        $info['goodsNew'] = M('goods')->where('goodsFlag=1 and createTime>"'.$weekDate.'"')->count();
        //新增订单
        $info['orderNew'] = M('orders')->where('orderFlag=1 and orderStatus >=0 and createTime>"'.$weekDate.'"')->count();
        //新增店铺
        $info['shopNew'] = M('Shops')->where('shopStatus = 1 and shopFlag=1 and createTime>"'.$weekDate.'"')->count();
        return $info;
    }

    public function getDate(){
        list($start,$end)=explode('-',str_replace(' ','',I('dr')));
        $start=str_replace('/','-',trim($start)).' 00:00:00';
        $end=str_replace('/','-',trim($end)).' 23:59:59';
        $where='createTime between "'.$start.'" and "'.$end.'"';
        $info['userNew'] = M('Users')->where('userFlag=1 and '.$where)->count();//新增用户
        
        //申请店铺
        $info['shopApply'] = M('Shops')->where('shopStatus >= 0 and shopFlag=1 and '.$where)->count();
        
        //新增商品
        $info['goodsNew'] = M('goods')->where('goodsFlag=1 and '.$where)->count();
        //新增订单
        $info['orderNew'] = M('orders')->where('orderFlag=1 and orderStatus >=0 and '.$where)->count();
        //新增店铺
        $info['shopNew'] = M('Shops')->where('shopStatus = 1 and shopFlag=1 and '.$where)->count();
        return $info;
    }
}